<template>
  <div>
    <el-form :inline="true">
      <el-form-item>
        <el-select v-model="searchForm.status" class="filter-item" placeholder="Please select status">
          <el-option v-for="item in statusOption" :key="item.key" :label="item.value" :value="item.key" />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button @click="getOrderList">搜索</el-button>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="saveHandle">创建</el-button>
      </el-form-item>
    </el-form>

    <div class="info">
      <el-divider><h1>我的订单</h1></el-divider>
      <br><br>
      <div v-if="this.tableData.length===0">
        <el-empty description="没有更多订单信息咯"></el-empty>
      </div>
      <div v-else>
        <el-table
            :data="tableData"
            tooltip-effect="dark"
            style="width: 100%"
            border
            stripe>

          <el-table-column
              prop="orderId"
              label="订单号"
              width="180">
          </el-table-column>
          <el-table-column
              prop="posPhone"
              label="联系电话"
              width="150"
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              prop="porName"
              label="收件人"
              width="100"
              show-overflow-tooltip>
          </el-table-column>
          <el-table-column
              prop="createDate"
              label="创建日期"
              width="200">
            <template slot-scope="scope">
              <el-text>{{scope.row.createDate | dateFilter}}</el-text>
            </template>
          </el-table-column>
          <el-table-column
              prop="price"
              label="快递费用"
              width="120"
              show-overflow-tooltip>
          </el-table-column>

          <el-table-column
              prop="defaults"
              label="当前状态"
              width="120"
          >
            <template slot-scope="scope">
              <el-tag size="small" v-if="scope.row.status===0" type="success">待处理</el-tag>
              <el-tag size="small" v-if="scope.row.status===1" type="success">待揽收</el-tag>
              <el-tag size="small" v-if="scope.row.status===2" type="success">待收货</el-tag>
              <el-tag size="small" v-if="scope.row.status===3" type="success">已签收</el-tag>
              <el-tag size="small" v-if="scope.row.status===4" type="warning">已取消</el-tag>
              <el-tag size="small" v-if="scope.row.status===5" type="danger">已退回</el-tag>
            </template>

          </el-table-column>
          <el-table-column
              prop="icon"
              label="操作">

            <template slot-scope="scope">
              <el-button type="text" @click="editHandle(scope.row.orderId)">编辑</el-button>
              <el-divider direction="vertical"></el-divider>
              <router-link :to="{
                path: '/mew/orderdetail',
                query: {
                  orderId: scope.row.orderId
                }
              }">
                <el-button type="text">详情</el-button>
              </router-link>
              <el-divider direction="vertical" v-if="scope.row.status===0 ||scope.row.status===1 || scope.row.status===2"></el-divider>
              <el-button type="text" v-if="scope.row.status===0 ||scope.row.status===1 || scope.row.status===2" @click="changeStatus(scope.row.orderId, 4)" >取消订单</el-button>
            </template>
          </el-table-column>

        </el-table>
      </div>
    </div>

    <!--新增对话框-->
    <el-dialog
        title="提示"
        :visible.sync="dialogVisible"
        width="600px"
        :before-close="handleClose">

      <el-form :model="editForm" :rules="editFormRules" ref="editForm">
        <el-form-item label="附近快递站点" prop="stationId" label-width="140px">
          <el-select v-model="editForm.stationId" class="filter-item" placeholder="Please select station" filterable>
            <el-option v-for="item in stationOption" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="寄件人姓名" prop="posName" label-width="140px">
          <el-input v-model="editForm.posName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="寄件人地区" prop="posArea" label-width="140px">
          <el-input v-model="editForm.posArea" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="寄件人地址" prop="posAddress" label-width="140px">
          <el-input v-model="editForm.posAddress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="寄件人联系方式" prop="posPhone" label-width="140px">
          <el-input v-model="editForm.posPhone" autocomplete="off" disabled></el-input>
        </el-form-item>
        <el-form-item label="收件人姓名" prop="posName" label-width="140px">
          <el-input v-model="editForm.porName" autocomplete="off">{{this.defaultAddress.name}}</el-input>
        </el-form-item>
        <el-form-item label="收件人地区" prop="posName" label-width="140px">
          <el-input v-model="editForm.porArea" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收件人地址" prop="posName" label-width="140px">
          <el-input v-model="editForm.porAddress" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="收件人联系方式" prop="posName" label-width="140px">
          <el-input v-model="editForm.porPhone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="包裹尺寸" prop="size" label-width="140px">
          <el-select v-model="editForm.size" class="filter-item" placeholder="Please select">
            <el-option v-for="item in sizeOption" :key="item.key" :label="item.value" :value="item.key" />
          </el-select>
        </el-form-item>
        <el-form-item label="包裹描述" prop="describes" label-width="140px">
          <el-input v-model="editForm.describes" autocomplete="off"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('editForm')">取 消</el-button>
        <el-button type="primary" @click="submitForm('editForm')">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import {getMyOrder, getDefaultAddress, getStationList, getOrderInfo, updateOrSaveOrder, change} from "@/api/mew";
import {getMyInfo} from "@/api/pc";

export default {
  name: "Order",
  data() {
    return{
      tableData: [],
      searchForm: {},
      statusOption: [
        { key: null, value: 'ALL'},
        { key: 0, value: '待处理'},
        { key: 1, value: '待揽收'},
        { key: 2, value: '待收货'},
        { key: 3, value: '已签收'},
        { key: 4, value: '已取消'},
        { key: 5, value: '已退回'}
      ],
      sizeOption: [
        { key: 0, value: '小件'},
        { key: 1, value: '中件'},
        { key: 2, value: '大件'},
        { key: 3, value: '超大件'},
      ],
      stationOption: [
      ],
      dialogVisible: false,
      editForm: {},
      editFormRules: {
        stationId: [
          {required: true, message: '请选择站点信息', trigger: 'blur'}
        ],
        posName: [
          {required: true, message: '请输入寄件人姓名', trigger: 'blur'}
        ],
        posArea: [
          {required: true, message: '请输入寄件人地区', trigger: 'blur'}
        ],
        posAddress: [
          {required: true, message: '请输入寄件人地址', trigger: 'blur'}
        ],
        posPhone: [
          {required: true, message: '请输入寄件人联系方式', trigger: 'blur'}
        ],
        porName: [
          {required: true, message: '请输入收件人姓名', trigger: 'blur'}
        ],
        porArea: [
          {required: true, message: '请输入收件人地区', trigger: 'blur'}
        ],
        porAddress: [
          {required: true, message: '请输入收件人地址', trigger: 'blur'}
        ],
        porPhone: [
          {required: true, message: '请输入收件人联系方式', trigger: 'blur'}
        ],
        size: [
          {required: true, message: '请选择包裹尺寸', trigger: 'blur'}
        ]
      },
      defaultAddress: [],
      myAddress: []
    }
  },
  created() {
    this.getOrderList()
    this.getDefault()
    this.getStation()
    this.getMyInfo()
  },
  methods: {
    getOrderList(){
      getMyOrder(this.searchForm.status).then(res =>{
        this.tableData = res.data.data
      })
    },
    getDefault(){
      getDefaultAddress().then(res =>{
        this.defaultAddress = res.data.data
      })
    },
    getMyInfo(){
      getMyInfo().then(res =>{
        this.myAddress = res.data.data
      })
    },
    getStation(){
      getStationList().then(res =>{
        res.data.data.forEach(stationItem =>{
          this.stationOption.push({ key: stationItem.accountId, value: stationItem.address})
        })
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.dialogVisible = false
      this.editForm = {}
    },
    handleClose() {
      this.resetForm('editForm')
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateOrSaveOrder(this.editForm).then(res =>{
            this.$message({
              showClose: true,
              message: '恭喜你，操作成功',
              type: 'success',
              onClose:() => {
                this.getOrderList()
              }
            });
            this.dialogVisible = false
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    saveHandle() {
      this.editForm.posName = this.myAddress.name
      this.editForm.posPhone = this.myAddress.phone
      this.editForm.porName = this.defaultAddress.name
      this.editForm.porArea = this.defaultAddress.area
      this.editForm.porAddress = this.defaultAddress.address
      this.editForm.porPhone = this.defaultAddress.phone
      this.dialogVisible = true
    },
    editHandle(id){
      getOrderInfo(id).then(res =>{
        this.editForm = res.data.data
        this.dialogVisible = true
      })
    },
    changeStatus(orderId, status){
      change(orderId, status).then(res =>{
        this.$message.success(res.data.data)
      })
    }
  }

}
</script>

<style scoped>

</style>